﻿using System.Collections.Generic;
using NativeSerializableDictionary;
using UnityEngine;

public class FSMServerMB : MonoBehaviour
{
    [SerializeField] private FSMStateMB currentState;
    [SerializeField] private List<FSMStateMB> stateList = new List<FSMStateMB>();
    private SerializableDictionary<string, FSMStateMB> stateDict = new SerializableDictionary<string, FSMStateMB>();

    private void Start()
    {
        //将stateList写入stateDict
        for (int i = 0; i < stateList.Count; i++)
        {
            stateDict.Add(stateList[i].IdentityName(), stateList[i]);
        }
    }

    private void ChangeState(FSMStateMB newState)
    {
        newState.Reset();
        currentState = newState;
    }

    private void Update()
    {
        currentState.UpdateState();
        string executeTransitions = currentState.ExecuteTransitions();
        if (executeTransitions != null)
        {
            ChangeState(stateDict[executeTransitions]);
        }
    }
}